﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using MvcPaging;

using HelfenHelfen.Logic.Interfaces;
using HelfenHelfen.Logic.Factories;
using HelfenHelfen.DataAccess.Entities;
using HelfenHelfen.Web.MVC.Helpers;
using HelfenHelfen.Web.MVC.Models.Views;

namespace HelfenHelfen.Web.MVC.Controllers
{
    public class BonusItemController : Controller
    {
        private const int defaultPageSize = 2;
        IBonusItemLogic bonusItemService = LogicFactory.CreateBonusItemLogic();
        IOrganisationLogic organisationService = LogicFactory.CreateOrganisationLogic();

        //
        // GET: /BonusItem/

        public ActionResult Index(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var bonusItemList = bonusItemService.GetAll();
            var pagedBonusItemList = bonusItemList.ToPagedList(currentPageIndex, defaultPageSize);

            return View(pagedBonusItemList);
        }

        //
        // GET: /BonusItem/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /BonusItem/Create

        [Authorize]
        public ActionResult Create()
        {
            BonusItemView bonusItemView = new BonusItemView()
            {
                BonusItem = null,
                Organisations = new SelectList(organisationService.GetAll(),"OrganisationGUID","Name")

            };
            return View(bonusItemView);
        } 

        //
        // POST: /BonusItem/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")] BonusItemView bonusItemView, HttpPostedFileBase pictureFile)
        {
            try
            {
                if (pictureFile != null)
                {
                    bonusItemView.BonusItem.Picture = ImageHelper.GetImageFromFile(pictureFile);
                }

                return CreateModel(bonusItemView);
            }
            catch
            {
                bonusItemView.Organisations = new SelectList(organisationService.GetAll(), "OrganisationGUID", "Name");
                return View("Create",bonusItemView);
            }
        }

        public ActionResult CreateModel(BonusItemView bonusItemView)
        {
            if (bonusItemView.BonusItem.ShortTesx.Length > 0)
            {
                bonusItemService.Insert(bonusItemView.BonusItem);
                return RedirectToAction("Index");
            }
            return View("Create", bonusItemView);
        }

        //
        // GET: /BonusItem/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            BonusItemView bonusItemView = new BonusItemView()
            {
                BonusItem = bonusItemService.Get(id),
                Organisations = new SelectList(organisationService.GetAll(), "OrganisationGUID", "Name")

            };
            return View("Edit",bonusItemView);
        }

        //
        // POST: /BonusItem/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(BonusItemView bonusItemView, HttpPostedFileBase pictureFile)
        {
            try
            {
                if (pictureFile != null)
                {
                    bonusItemView.BonusItem.Picture = ImageHelper.GetImageFromFile(pictureFile);
                }
                else //use the existing image
                {
                    bonusItemView.BonusItem.Picture = bonusItemService.Get(bonusItemView.BonusItem.BonusItemGUID).Picture;
                }

                bonusItemService.Update(bonusItemView.BonusItem);

 
                return RedirectToAction("Index");
            }
            catch
            {
                bonusItemView.Organisations = new SelectList(organisationService.GetAll(), "OrganisationGUID", "Name");
                return View("Edit", bonusItemView);
            }
        }

        public ActionResult Delete(Guid id)
        {
            var bonusItem = bonusItemService.Get(id);
            return View("Delete", bonusItem);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(Guid id, string confirmButton)
        {
            try
            {
                bonusItemService.Delete(id);
            }
            catch
            {
                return View("Delete", bonusItemService.Get(id));
            }
            
            return RedirectToAction("Index");
        }

        public ActionResult ShowBonusItemPicture(Guid bonusItemId)
        {
            byte[] picture = bonusItemService.Get(bonusItemId).Picture;
            return File(picture, "image/jpg");
        }
    }
}
